package com.changgou.system.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

//jwt工具类
public class JwtUtil {

    //设置有效期
    public static final Long JWT_TTL = 3600000L;

    //设置秘钥明文
    public static final String JWT_KEY = "itcast";


    /**
     * @Description:    生成加密后的秘钥
     * @Author:        wangqingsong
     * @param:           []
     * @return:          javax.crypto.SecretKey
     * @CreateDate:     2020/11/21 16:47
     */
    public static SecretKey generalKey() {

        byte[] decode = Base64.getDecoder().decode(JwtUtil.JWT_KEY);

        SecretKey secretKey = new SecretKeySpec(decode, 0, decode.length, "AES");

        return secretKey;
    }


    public static Claims parseJwt(String jwt) {

        SecretKey secretKey = generalKey();

        Claims claims = Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();

        return claims;
    }

}
